﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using UNDP_Projects.Data_Access;
using UNDP_Projects.Classes;

namespace UNDP_Projects
{
    public partial class Archived_Project_Reports : PdfCreator
    {
        protected void Page_PreInit(object sender, EventArgs e)
        {
            /*if (User.IsInRole("Project"))
            {
                MasterPageFile = "~/MasterPages/Project.Master";
            }
            if (User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/Programme.Master";
            }
            if (User.IsInRole("Administrator"))
            {
                MasterPageFile = "~/MasterPages/CO.Master";
            }
            if (User.IsInRole("Project") && User.IsInRole("Programme"))
            {
                MasterPageFile = "~/MasterPages/ProgProject.Master";
            }*/
        }

        UNDPEntities db = new UNDPEntities();

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ArchivedProjectsGridView2_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "pdf")
            {
                //Get the selected row
                int currentRowIndex = Convert.ToInt32(e.CommandArgument);
                GridViewRow row = ArchivedProjectsGridView2.Rows[currentRowIndex];

                //Get the Datakeynames for the selected row
                string selPROJ = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["project_id"].ToString();
                string selYr = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["year_id"].ToString();
                string selRPRD = this.ArchivedProjectsGridView2.DataKeys[currentRowIndex].Values["report_period_id"].ToString();

                long proj = long.Parse(selPROJ);
                long reportp = long.Parse(selRPRD);
                long year = long.Parse(selYr);

                //PdfCreator NewPdfDoc = new PdfCreator();
                //NewPdfDoc
                CreatePDF(proj, reportp, year);
            }
        }

        protected void ArchivedProjectsGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            //Get the selected row
            int currentRowIndex = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = ArchivedProjectsGridView1.Rows[currentRowIndex];

            //Get the Datakeynames for the selected row
            string selPROJ = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["project_id"].ToString();
            string selYr = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["year_id"].ToString();
            string selRPRD = this.ArchivedProjectsGridView1.DataKeys[currentRowIndex].Values["report_period_id"].ToString();

            long proj = long.Parse(selPROJ);
            long reportp = long.Parse(selRPRD);
            long year = long.Parse(selYr);

            if (e.CommandName == "pdf")
            {
                //NewPdfDoc
                CreatePDF(proj, reportp, year);
            }
            else if (e.CommandName == "return")
            {
                try
                {
                    project_reports pr = db.project_reports.SingleOrDefault(
                                            mypr => mypr.project_id == proj &&
                                            mypr.year_id == year &&
                                            mypr.report_period_id == reportp);

                    pr.archived = false;
                    pr.approved = true;

                    db.SaveChanges();
                    ArchivedProjectsGridView1.DataBind();
                }
                catch (Exception ex)
                {
                    Error.Text = "Error : " + ex.Message + " : " + ex.StackTrace;
                    Error.Visible = true;
                }
            }
        }
    }
}